<%@ page import="com.interfaces.PrivilegeController" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>角色功能设置</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

    <%
        String path = request.getContextPath();
        path = "/".equals(path)?"":path;
        request.setAttribute("path", path);
    %>
    <script type="text/javascript">
        var path = "${path}";
    </script>

    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="${path}/public/adminlte/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="${path}/public/font-awesome-4.7.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="${path}/public/ionicons/ionicons.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" href="${path}/public/adminlte/plugins/datatables/dataTables.bootstrap.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="${path}/public/adminlte/dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect.
    -->
    <link rel="stylesheet" href="${path}/public/adminlte/dist/css/skins/_all-skins.min.css">

    <link rel="stylesheet" href="${path}/css/common.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<!--
BODY TAG OPTIONS:
=================
Apply one or more of the following classes to get the
desired effect
|---------------------------------------------------------|
| SKINS         | skin-blue                               |
|               | skin-black                              |
|               | skin-purple                             |
|               | skin-yellow                             |
|               | skin-red                                |
|               | skin-green                              |
|---------------------------------------------------------|
|LAYOUT OPTIONS | fixed                                   |
|               | layout-boxed                            |
|               | layout-top-nav                          |
|               | sidebar-collapse                        |
|               | sidebar-mini                            |
|---------------------------------------------------------|
-->
<body>
<div class="container">

           
            <!-- /.box -->
            <div class="box box-primary">
                <!-- /.box-header -->
                <div class="box-body">
                	<label>已有功能</label><button type="submit" id="batDel" class="btn btn-primary pull-right">删除</button>
                <!-- 
  				<button type="button" id="add-btn">添加</button>
  				<button type="button" id="batch-edit-btn">编辑</button>
  				<button type="button" id="batch-save-btn">保存</button>
  				<button type="button" id="batch-clear-btn">清空</button>
  				 -->
  					<input type="hidden" id="id" value="${roleInfo.id}">
                    <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
				</table>
                </div>
                <!-- /.box-body -->
            </div>
            <div class="box box-primary">
                <!-- /.box-header -->
                <div class="box-body">
                	<label>未有功能</label><button type="submit" id="batAdd" class="btn btn-primary pull-right">添加</button>
                    <table id="table1" class="table table-striped table-bordered" cellspacing="0" width="100%">
				</table>
                </div>
                <!-- /.box-body -->
            </div>
</div>

<!-- ./wrapper -->

<div class="modal fade" id="modelContainer" role="basic" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 2.2.3 -->
<div align="left"><script src="${path}/public/adminlte/plugins/jQuery/jquery-2.2.3.min.js"></script> 
<!-- Bootstrap 3.3.6 --> 
<script src="${path}/public/adminlte/bootstrap/js/bootstrap.min.js"></script> 
<!-- DataTables --> 
<script src="${path}/public/adminlte/plugins/datatables/jquery.dataTables.min.js"></script> 
<script src="${path}/public/adminlte/plugins/datatables/dataTables.bootstrap.min.js"></script> 
<!-- SlimScroll --> 
<script src="${path}/public/adminlte/plugins/slimScroll/jquery.slimscroll.min.js"></script> 
<!-- FastClick --> 
<script src="${path}/public/adminlte/plugins/fastclick/fastclick.js"></script> 
<!-- AdminLTE App --> 
<script src="${path}/public/adminlte/dist/js/app.min.js"></script> 
<script src="${path}/js/validata.js"></script>
 
<!-- Optionally, you can add Slimscroll and FastClick plugins.
     Both of these plugins are recommended to enhance the
     user experience. Slimscroll is required when using the
     fixed layout. --> 
<!-- page script --> 
</div><script>
	var table;
	var table1;
	
    $(function(){
    	var id = $('#id').val();
       table = $('#table').DataTable({
       		"paging": true,
            "lengthChange": true,
            "searching": false,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "processing":true,//显示处理中
        	"serverSide":true,//每次都请求服务器，包括搜索、切换每页显示数、翻页
        	"stateSave": true,//刷新时保持翻页状态，table.fnDraw(false);//刷新保持分页状态
        	"pageLength": 10,  //首次加载的数据条数
           "ajax": {
               "url": "${path}<%=PrivilegeController.URL_SEARCH%>?id="+id+"&"+Math.random(),
               //"dataSrc": "data",//默认为data
               "type": "post",
               "error":function(){},
              // "success":function(){}
           },
           "columns": [
           			{
            		"data":"id",
            		"title":"<input type='checkbox' class='check' id='checkAll'>",
            		"defaultContent":"",
            		"render":function(data, type, full, meta){
            			return '<input type="checkbox" name="subcheck" class="checkchild"  value="' + data + '" />';
            		},
            		"bSortable": false
            		},
               { "data": "code","title":"功能编码","defaultContent":""},
               { "data": "value","title":"功能值","defaultContent":""},
               { "data": "no_limit_access","title":"是否无限制","defaultContent":""},
               { "data": "need_login","title":"是否需要登录","defaultContent":""},
               { "data": "comments", "title":"功能说明","defaultContent":""}
           ],
            "columnDefs": [
                  {
                      "targets": [3],
                      "data": "no_limit_access",
                      "render": function(data, type, full) { 
                       return data==0?"受限制":"无限制";
                      }
                  },
                  {
                  	  "targets": [4],
                      "data": "need_login",
                      "render": function(data, type, full) { 
                       return data==0?"无需登录":"需要登录";
                      }
                  }
              ],
           //"aaSorting": [  
			//	[ 0, "asc" ]  
			//],
           "language": {
               "sProcessing": "处理中...",
               "sLengthMenu": "显示 _MENU_ 项结果",
               "sZeroRecords": "没有匹配结果",
               "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
               "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
               "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
               "sInfoPostFix": "",
               "sSearch": "搜索:",
               "sUrl": "",
               "sEmptyTable": "表中数据为空",
               "sLoadingRecords": "载入中...",
               "sInfoThousands": ",",
               "oPaginate": {
                   "sFirst": "首页",
                   "sPrevious": "上页",
                   "sNext": "下页",
                   "sLast": "末页"
               },
               "oAria": {
                   "sSortAscending": ": 以升序排列此列",
                   "sSortDescending": ": 以降序排列此列"
               }
           }
       });
       
       table1 = $('#table1').DataTable({
       		"paging": true,
            "lengthChange": true,
            "searching": false,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "processing":true,//显示处理中
        	"serverSide":true,//每次都请求服务器，包括搜索、切换每页显示数、翻页
        	"stateSave": true,//刷新时保持翻页状态，table.fnDraw(false);//刷新保持分页状态
        	"pageLength": 10,  //首次加载的数据条数
           "ajax": {
               "url": "${path}<%=PrivilegeController.URL_SEARCH_NOT%>?id="+id+"&"+Math.random(),
               //"dataSrc": "data",//默认为data
               "type": "post",
               "error":function(){},
              // "success":function(){}
           },
           "columns": [
           		{
            		"data":"id",
            		"title":"<input type='checkbox' class='check' id='checkAll1'>",
            		"defaultContent":"",
            		"render":function(data, type, full, meta){
            			return '<input type="checkbox" name="subcheck1" class="checkchild1"  value="' + data + '" />';
            		},
            		"bSortable": false
            	},
               { "data": "code","title":"功能编码","defaultContent":""},
               { "data": "value","title":"功能值","defaultContent":""},
               { "data": "no_limit_access","title":"是否无限制","defaultContent":""},
               { "data": "need_login","title":"是否需要登录","defaultContent":""},
               { "data": "comments", "title":"功能说明","defaultContent":""}
           ],
            "columnDefs": [
                  {
                      "targets": [3],
                      "data": "no_limit_access",
                      "render": function(data, type, full) { 
                       return data==0?"受限制":"无限制";
                      }
                  },
                  {
                  	  "targets": [4],
                      "data": "need_login",
                      "render": function(data, type, full) { 
                       return data==0?"无需登录":"需要登录";
                      }
                  }
              ],
           //"aaSorting": [  
			//	[ 0, "asc" ]  
			//],
           "language": {
               "sProcessing": "处理中...",
               "sLengthMenu": "显示 _MENU_ 项结果",
               "sZeroRecords": "没有匹配结果",
               "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
               "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
               "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
               "sInfoPostFix": "",
               "sSearch": "搜索:",
               "sUrl": "",
               "sEmptyTable": "表中数据为空",
               "sLoadingRecords": "载入中...",
               "sInfoThousands": ",",
               "oPaginate": {
                   "sFirst": "首页",
                   "sPrevious": "上页",
                   "sNext": "下页",
                   "sLast": "末页"
               },
               "oAria": {
                   "sSortAscending": ": 以升序排列此列",
                   "sSortDescending": ": 以降序排列此列"
               }
           }
       });
 		
 		$("#checkAll").click(function () {
	      var check = $(this).prop("checked");
	      $(".checkchild").prop("checked", check);
		});	
		
		$("#checkAll1").click(function () {
	      var check = $(this).prop("checked");
	      $(".checkchild1").prop("checked", check);
		});
		
		$("#batDel").click(function () {
	    	var checkedNum = $("input[name='subcheck']:checked").length;  
	    	if(checkedNum==0){  
	        	alert("删除功能时请至少选择一个功能!");  
	            return false;  
	        }  
	       	if(confirm("确定删除该角色下的功能吗?")){  
	        	var checkedList = new Array();  
	            $("input[name='subcheck']:checked").each(function(){  
	            	checkedList.push($(this).val());  
	           	});  
	            $.ajax({  
	            	type:"POST",  
	               	url:"${path}<%=PrivilegeController.URL_DELETE_PRIVILEGE_BAT%>?id="+$('#id').val()+"&"+Math.random(),  
	                data:{"roleJson":checkedList.toString()},    
	                success:function(result){  
	                	  var re = JSON.parse(result);
	                   	  if(re.returnCode=="0"){
	                   	 	//$("[name='checkbox2']:checkbox").attr("checked",false);  
	                       	table.draw(false);
	                       	table1.draw(false);
	                   	  }else{
	                   	   	alert(re.msg);
	                   	  	}
	                   },  
	           		error:function(request) {      // 设置表单提交出错
	                	alert(request);  //登录错误提示信息
	            	}
	          	});  
	       	}
		});
		
		$("#batAdd").click(function () {
	      var checkedNum = $("input[name='subcheck1']:checked").length;  
	    	if(checkedNum==0){  
	        	alert("添加功能时请至少选择一个功能!");  
	            return false;  
	        }  
	       	if(confirm("确定为该角色添加所选功能吗?")){  
	        	var checkedList = new Array();  
	            $("input[name='subcheck1']:checked").each(function(){  
	            	checkedList.push($(this).val());  
	           	});  
	            $.ajax({  
	            	type:"POST",  
	               	url:"${path}<%=PrivilegeController.URL_ADD_UPDATE_PRIVILEGE_BAT%>?id="+$('#id').val()+"&"+Math.random(),  
	                data:{"roleJson":checkedList.toString()},    
	                success:function(result){  
	                	  var re = JSON.parse(result);
	                   	  if(re.returnCode=="0"){
	                   	 	//$("[name='checkbox2']:checkbox").attr("checked",false);  
	                       	//location.reload();
	                       	table.draw(false);
	                       	table1.draw(false);
	                   	  }else{
	                   	   	alert(re.msg);
	                   	  	}
	                   },  
	           		error:function(request) {      // 设置表单提交出错
	                	alert(request);  //登录错误提示信息
	            	}
	          	});  
	       	}
		});
		
   });
   
   
</script>
</body>
</html>
